package com.jic.interaction.ChinaPost.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jic.interaction.common.enums.RedisKey;
import com.jic.interaction.common.utils.HttpClientUtil;
import com.jic.interaction.common.utils.RedisService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @author liding
 * @version v1
 * @date 2020/10/29 15:37
 */
@Component
@Slf4j
@EnableScheduling
public class AccessTokenTask {

    @Autowired
    private RedisService redisService;

    @Value("${wz.appid}")
    private String appid;

    @Value("${wz.secret}")
    private String secret;

    @Value("${wz.chinaPostUrl}")
    private String url;

    /**
     * 更新AccessToken
     */
    @Scheduled(fixedDelay = 50 * 60 * 1000)
    public void updateAccessTokenTask() throws InterruptedException, ExecutionException, IOException {
        log.info("更新AccessToken定时任务");
        Map<String, String> params2 = new HashMap<>();
        params2.put("app_id","W3408745"); //添加appid
        params2.put("secret","lqdvKh3BHs0DXofDT6mDv2EFNT4Vl8MlqLivdEXB3WSQoTFRoL0ttdtYYEJ20JtP"); //添加token
        params2.put("grant_type","client_credential");
        params2.put("version","1.0.0");
        String body2 = HttpClientUtil.doRequestGet("http://139.217.228.165:8070/"+"m1/api/oauth2/access_token",params2,null);
        if (StringUtils.isNotEmpty(body2)) {
            System.out.println(body2+"...........token");
        }
        String token = JSON.parseObject(body2).getString("access_token");
        redisService.setValue(RedisKey.WZ_ACCESS_TOKEN,token,18000L);
        Map<String, String> params = new HashMap<>(); //创建map
        params.put("app_id","W3408745"); //添加appid
        params.put("access_token",token); //添加token
        params.put("type","SIGN");
        params.put("version","1.0.0");
        String body = HttpClientUtil.doRequestGet("http://139.217.228.165:8070/"+"m1/api/oauth2/api_ticket",params,null); //请求
        JSONObject jsonObject= JSON.parseObject(body);
        JSONArray tickets = jsonObject.getJSONArray("tickets");
        JSONObject ticket = tickets.getJSONObject(0);
        String singTicket = ticket.getString("value");
        redisService.setValue(RedisKey.WZ_SING_TICKET,singTicket,18000L);
        if (StringUtils.isNotEmpty(body)) {
            System.out.println(body+"..........api_ticket");
        } else {
            log.error("更新SingTicket请求失败");
        }
    }


    @SneakyThrows
    public static void main(String[] args) {
        Map<String, String> params2 = new HashMap<>();
        params2.put("app_id","W3408745"); //添加appid
        params2.put("secret","lqdvKh3BHs0DXofDT6mDv2EFNT4Vl8MlqLivdEXB3WSQoTFRoL0ttdtYYEJ20JtP"); //添加token
        params2.put("grant_type","client_credential");
        params2.put("version","1.0.0");
        String body2 = HttpClientUtil.doRequestGet("http://139.217.228.165:8070/"+"m1/api/oauth2/access_token",params2,null);
        if (StringUtils.isNotEmpty(body2)) {
            System.out.println(body2+"...........token");
        }
        Map<String, String> params = new HashMap<>(); //创建map
        params.put("app_id","W3408745"); //添加appid
        params.put("access_token",JSON.parseObject(body2).getString("access_token")); //添加token
        params.put("type","SIGN");
        params.put("version","1.0.0");
        String body = HttpClientUtil.doRequestGet("http://139.217.228.165:8070/"+"m1/api/oauth2/api_ticket",params,null); //请求
        if (StringUtils.isNotEmpty(body)) {
            System.out.println(body+"..........api_ticket");
        } else {
            log.error("更新SingTicket请求失败");
        }

    }

}


